﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.Text;
using EzDesk.Apps.Data.Attendance;
using EzDesk.Apps.Model.Attendance;
using EzDesk.Apps.Interface.Attendance;

using EzDesk.EzAppApi.Utility;
using EzDesk.Utility.Generic;
using EzDesk.Utility.ServiceStatusInfo;
using Microsoft.Practices.Unity;
using EzDesk.Utility.Log;
using EzDesk.Utility.TimerService;


namespace EzDesk.Apps.ServiceImpl.Attendance
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public partial class AttendanceService : IAttendanceService
    {
        private IEzApiClient _ezApiClient
        {
            get { return CacheableEzApiClient.Instance; }
        }

        #region [ private members ]
        private static bool isInitialized = false;

        private const string FeedTemplateStr_attendance_applyLeave = "attendance_applyLeave";
        private const string FeedTemplateStr_attendance_auditLeaveSucc = "attendance_auditLeaveSucc";
        private const string FeedTemplateStr_attendance_auditLeaveRejected = "attendance_auditLeaveRejected";
        private const string FeedTemplateStr_attendance_dailyAttendanceStatics = "attendance_dailyAttendanceStatics";
        private const string FeedTemplateStr_attendance_dailyAttendanceReport = "attendance_dailyAttendanceReport";
        private const string FeedTemplateStr_attendance_applyEvaluationModify = "attendance_applyEvaluationModify";
        private const string FeedTemplateStr_attendance_evaluationModified = "attendance_evaluationModified";
        private const string FeedTemplateStr_attendance_checkedAttendance = "attendance_checkedAttendance";
        private const string FeedTemplateStr_attendance_evaluationRejected = "attendance_evaluationRejected";

        AttendanceConfig attendanceConfig;

        #endregion

        #region [ Constructors ]
        public AttendanceService()
        {
            LogEx.TRACE("AttendanceService", "初始化AttendanceService");

            attendanceConfig = new AttendanceConfig();
            initializeAttendanceConfig(attendanceConfig);

            initializeTimerSerivce();
        }

         
        #endregion


 

        #region [ about attendance configs ]
        public StatusInfo<AttendanceConfig> GetAttendanceConfig()
        {
            return StatusInfo.Wrap(
                () => getAttendanceConfig());
        }

        #endregion

        #region [ private methods ]

        private AttendanceConfig getAttendanceConfig()
        {
            return attendanceConfig;
        }

        private void initializeAttendanceConfig(AttendanceConfig attendanceConfig)
        {
            // 上午9点上班
            attendanceConfig.StdOnDutyTime = new SimpleTime() { Hour = 9, Miniute = 0 };

            // 下午6点上班
            attendanceConfig.StdOffDutyTime = new SimpleTime() { Hour = 18, Miniute = 0 };

            // 标准上班延迟时间 5mins
            attendanceConfig.StdOnDutyTimeDelay = TimeSpan.FromMinutes(5);

            // 上班弹性提前时间 30mins
            attendanceConfig.OnDutyOffsetForward = TimeSpan.FromMinutes(30);

            // 上班弹性延后时间 0mins
            attendanceConfig.OnDutyOffsetAfterward = TimeSpan.FromMinutes(0);

            // 午休时间段 1小时
            attendanceConfig.LunchBreakStartOn = new SimpleTime() { Hour = 11, Miniute = 30 };

            attendanceConfig.LunchBreakEndOn = new SimpleTime() { Hour = 12, Miniute = 30 };

            // 标准每日 工时
            attendanceConfig.StdManhours = TimeSpan.FromHours(8);
        }




        private void initializeTimerSerivce()
        {
            if (!isInitialized)
            {
                var config = getAttendanceConfig();
                var ondutyTime = config.StdOnDutyTime.ToDateTime().Add(-config.OnDutyOffsetForward).TimeOfDay;
                LogEx.TRACE("AttendanceService", "添加TimerTasks");
                //TimerServiceHost.Instance.Begin();
                isInitialized = true;
            }
        }


        

        #endregion



    }
}
